/*
 * Copyright (c) 2015 smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.web.property.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.common.tools.EnumUtil;
import com.smarthome.core.user.UserResourceService;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.bean.ShUserResource;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.enums.ShUserResourceTypeFlagEnum;
import com.smarthome.domain.vo.boss.Tree;
import com.smarthome.web.property.utils.UserHolder;

/**
 * resource controller层
 * @author KongXiangyu
 * @since 2015-08-05
 */
@Controller
@RequestMapping(value = "/resourceController")
public class ResourceController {

    @Resource
    private UserResourceService userResourceService;

    /**
     * 获得资源树(资源类型为菜单类型)
     * 
     * 通过用户ID判断，他能看到的资源
     * 
     * @param session
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public List<Tree> tree() {
        return userResourceService.tree(UserHolder.getUser(), ShUserResourceTypeFlagEnum.MENU);
    }

    /**
     * 获得资源树(包括所有资源类型)
     * 
     * 通过用户ID判断，他能看到的资源
     * 
     * @param session
     * @return
     */
    @RequestMapping("/allTree")
    @ResponseBody
    public List<Tree> allTree() {
        return userResourceService.tree(null, null);
    }

    /**
     * 跳转到资源添加页面
     * 
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        request.setAttribute("resourceTypeList", EnumUtil.getEnumList(ShUserResourceTypeFlagEnum.class));
        return "/system/resourceAdd";
    }

    /**
     * 添加资源
     * 
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(ShUserResource resource) {
        userResourceService.add(resource);
        return Result.success("添加成功！");
    }

    /**
     * 跳转到资源编辑页面
     * 
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request, Long id) {
        request.setAttribute("resourceTypeList", EnumUtil.getEnumList(ShUserResourceTypeFlagEnum.class));
        request.setAttribute("resource", userResourceService.queryUserResourceById(id));
        return "/system/resourceEdit";
    }

    /**
     * 编辑资源
     * 
     * @param resource
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(ShUserResource resource) {
        userResourceService.edit(resource);
        return Result.success("编辑成功！");
    }

    /**
     * 获得资源列表
     * 
     * 通过用户ID判断，他能看到的资源
     * 
     * @return
     */
    @RequestMapping("/treeGrid")
    @ResponseBody
    public List<ShUserResource> treeGrid() {
        return userResourceService.treeGrid(null);
    }

    /**
     * 删除资源
     * 
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Long id) {
        userResourceService.delete(id);
        return Result.success("删除成功！");
    }

}